package utils

import (
	"time"
)

// GetTomorrow 获取指定时间的00：00
func GetTomorrow(now time.Time) (tomorrowMidnight time.Time) {
	// 设置时间为 00:00:00
	tomorrowMidnight = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
	return
}

// GetTodayTimeByTimeSub 获取两个时间的时间差（time.Duration 秒级）
// 左小右大
func GetTodayTimeByTimeSub(time1, byTime time.Time) (seconds time.Duration) {

	// 计算时间差
	duration := byTime.Sub(time1)

	// 获取秒数
	seconds = time.Duration(duration.Seconds()) * time.Second
	return
}

func GetTodayTimeByTimeSubDays(time1, byTime time.Time) (day int) {

	// 计算时间差
	duration := byTime.Sub(time1)

	// 获取天数
	day = int(duration.Hours() / 24)
	return
}

func GetNextMouthNo1(currentTime time.Time) (duration time.Duration) {

	// 获取下个月的第一天
	nextMonth := currentTime.AddDate(0, 1, 0)
	firstDayOfNextMonth := time.Date(nextMonth.Year(), nextMonth.Month(), 1, 0, 0, 0, 0, nextMonth.Location())

	// 计算时间差
	duration = firstDayOfNextMonth.Sub(currentTime) * time.Second
	return
}

// Date 设置时间(1524799394,"02/01/2006 15:04:05 PM")
func Date(format string, timestamp int64) string {
	loc, _ := time.LoadLocation("Asia/Shanghai")
	if format == "" {
		format = "2006-01-02 15:04:05"
	}
	if timestamp == 0 {
		timestamp = time.Now().In(loc).Unix()
	}
	return time.Unix(timestamp, 0).In(loc).Format(format)
}

// GetNowTime 当前时间
func GetNowTime() time.Time {
	loc, _ := time.LoadLocation("Asia/Shanghai")
	return time.Now().In(loc)
}

// GetNowSecond 秒
func GetNowSecond() int64 {
	return GetNowTime().Unix()
}

func TimeMouthAndDayAndYear(newTime int64) (timeStr string) {
	time1 := time.Unix(newTime, 0)
	timeStr = time1.Format("2006-01-02")
	return
}

func Time2String(t time.Time) string {
	return t.Format("2006-01-02")
}

// AddVipEndTime 添加vip结束时间时长
// timestamp：当前vip结束时间戳（秒）
// day：天数
func AddVipEndTime(timestamp int64, day int) int64 {
	t := time.Unix(timestamp, 0)
	startOfDay := t.AddDate(0, 0, day)
	startOfDay = time.Date(startOfDay.Year(), startOfDay.Month(), startOfDay.Day(), 23, 59, 59, 0, startOfDay.Location())
	return startOfDay.Unix()
}

// GetCurrentDate 获取当前时间，不含时间
func GetCurrentDate() time.Time {
	currentDate := time.Now()
	return time.Date(currentDate.Year(), currentDate.Month(), currentDate.Day(), 0, 0, 0, 0, time.Local)
}

func MonthBegin() time.Time {
	now := time.Now()
	return time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
}

func MonthEnd() time.Time {
	now := time.Now()
	year, month, _ := now.Date()

	// 获取下个月的第一天
	nextMonth := time.Date(year, month+1, 1, 23, 59, 59, 0, now.Location())

	// 回退一天，即为当前月的最后一天
	lastDayOfMonth := nextMonth.Add(-24 * time.Hour)

	return lastDayOfMonth
}
